package com.redhat.auth.web.account;

import java.util.Date;

import javax.validation.Valid;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import com.redhat.auth.application.AccountApplication;
import com.redhat.auth.domain.Account;
import com.redhat.redwork.coc.RedworkAction;
import com.redhat.redwork.exception.ServiceException;

/**
 * 
 * @author lingen.liu account的action类
 */
public class AccountAction extends ActionSupport implements RedworkAction {

	@Valid
	private Account account;

	private String passwordConfirm;

	private int isLocked;

	@Autowired
	private AccountApplication accountApplication;

	/**
	 * 新增一个Account对象
	 * 
	 * @return
	 * @throws Exception
	 */
	public String create() throws Exception {
		accountApplication.add(account);
		return "json";
	}

	/**
	 * 修改一个帐号对象
	 * 
	 * @return
	 * @throws Exception
	 */
	public String modifyAccount() throws Exception {

		Account acc = accountApplication.getAccount(account.getId());
		acc.setEmail(account.getEmail());
		acc.setRealName(account.getRealName());
		acc.setDescription(account.getDescription());
		accountApplication.update(acc);
		return "json";
	}

	public String modify() {
		long id = Long.parseLong(ServletActionContext.getRequest()
				.getParameter("id"));
		account = accountApplication.getAccount(id);
		this.isLocked = account.isLock() == false ? 1 : 0;
		return "modify";
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	public AccountApplication getAccountApplication() {
		return accountApplication;
	}

	public void setAccountApplication(AccountApplication accountApplication) {
		this.accountApplication = accountApplication;
	}

	public int getIsLocked() {
		return isLocked;
	}

	public void setIsLocked(int isLocked) {
		this.isLocked = isLocked;
	}

}
